package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vincent.kdp.R;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.bean.response.NoticeNumOutput;
import com.vincent.kdp.ui.notice.repository.NoticeRepository;
import com.vincent.kdp.view.BadgeView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/13 0013.
 */
public class CenterModuleAdapter extends RecyclerView.Adapter<CenterModuleAdapter.ViewHolder> {

    private String[] moduleNames = new String[]{"通知信","今日盘货","今日收货","客户意向","消费搜索" ,"柜台管理","退出"};
    private int[] moduleResIds = new int[]{R.mipmap.ic_news,R.mipmap.i_1,R.mipmap.receive,R.mipmap.ic_notification,
            R.mipmap.ic_vips,R.mipmap.ic_vips,R.mipmap.ic_vips,R.mipmap.ic_vips};

    private Context context;
    private OnItemClicker onItemClicker;

    private static int NOTICE_TYPE=2;
    private static int LOGOUT_TYPE=1;
    private static int NORMAL_TYPE=0;

    private NoticeRepository noticeRepository=new NoticeRepository();
    BadgeView badgeView;
    public CenterModuleAdapter(Context context, OnItemClicker onItemClicker) {
        this.context = context;
        this.onItemClicker = onItemClicker;
        badgeView= new BadgeView(context);
    }

    private void reqNoticeNum(final TextView target){
        noticeRepository.reqHavingNoticeNum(new  RequestCallBack< NoticeNumOutput>(){
            @Override
            public void onSuccess(NoticeNumOutput numOutput) {
                badgeView.setTargetView(target);
                badgeView.setBadgeGravity(Gravity.CENTER);
                badgeView.setBadgeCount(4);
                badgeView.setBadgeCount(numOutput.getData().getIsShow());
            }

            @Override
            public void onFalse(String msg) {

            }
        });
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        if(viewHolder instanceof ItemViewHolder){
            ItemViewHolder holder=(ItemViewHolder)viewHolder;
            holder.mModuleImg.setImageResource(moduleResIds[position]);
            holder.mModuleNameTv.setText(moduleNames[position]);
            if(position==0){
                reqNoticeNum(holder.moduleBadgeTv);
            }
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View logout_view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_rv_center_module_logout, parent, false);
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_center_module, parent,
                false);

        if (viewType == LOGOUT_TYPE)
            return new ViewHolder(logout_view, onItemClicker);
        return new ItemViewHolder(view, onItemClicker);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 6) {
            return LOGOUT_TYPE;
        }
        return NORMAL_TYPE;
    }

    @Override
    public int getItemCount() {
        return moduleNames.length;
    }

    public class ItemViewHolder extends ViewHolder {
        @Bind(R.id.module_img)
        ImageView mModuleImg;
        @Bind(R.id.module_name_tv)
        TextView mModuleNameTv;
        @Bind(R.id.module_badge_tv)
        TextView moduleBadgeTv;

        private OnItemClicker onCheckedItemClicker;

        public ItemViewHolder(View itemView, OnItemClicker onCheckedItemClicker) {
            super(itemView,onCheckedItemClicker);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            this.onCheckedItemClicker = onCheckedItemClicker;
        }

        @Override
        public void onClick(View v) {
            onCheckedItemClicker.operate(v, getAdapterPosition());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private OnItemClicker onCheckedItemClicker;

        public ViewHolder(View itemView, OnItemClicker onCheckedItemClicker) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            this.onCheckedItemClicker = onCheckedItemClicker;

        }

        @Override
        public void onClick(View v) {
            onCheckedItemClicker.operate(v, getAdapterPosition());
        }
    }

    public interface OnItemClicker {
        void operate(View view, int position);
    }


}
